package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.exception.YYGHException;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.result.R;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 */
@Api(tags = "管理员系统-医院设置信息")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

   @Autowired
   private HospitalSetService hospitalSetService;

    //修改锁定状态
    @PutMapping("/update/status/{id}/{status}")
    public R updateStatus(@PathVariable Long id,@PathVariable Integer status){
       // log.info("thread:"+Thread.currentThread().getId()+":updateStatus:"+id+":"+status);
        UpdateWrapper<HospitalSet> updateWrapper=new UpdateWrapper<HospitalSet>();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",status);
        hospitalSetService.update(updateWrapper);
        return R.ok();
    }

   //批量删除
    @DeleteMapping("/batch/delete")
    public R batchDelete(@RequestBody List<Long> ids){
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    @ApiOperation(value ="修改之去详情页面")
    @GetMapping("/detail/{id}")
    public R detail(@ApiParam(name = "id",value="医院设置id",required = true)@PathVariable Long id){
        return R.ok().data("item",hospitalSetService.getById(id));
    }

    //修改：
    @PutMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }


   //添加
    @PostMapping("/save")
    public R addHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+random.nextInt(1000)+""));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }


   //带查询条件的分页
    @PostMapping("/page/{current}/{size}")
    public R getHospitalSetPage(@PathVariable("current") Long current,
                                @PathVariable("size") Long size,
                                @RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> hospitalSetPage=new Page<HospitalSet>(current,size);
        QueryWrapper<HospitalSet> queryWrapper=new QueryWrapper<HospitalSet>();
        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname())){
            queryWrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())){
            queryWrapper.like("hoscode",hospitalSetQueryVo.getHoscode());
        }
        hospitalSetPage = hospitalSetService.page(hospitalSetPage, queryWrapper);
        return R.ok().data("total",hospitalSetPage.getTotal()).data("rows",hospitalSetPage.getRecords());

    }
    // http://localhost:8201/admin/hosp/hospitalSet/findAll
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有的医院设置信息")
    public R findAll(){
        try{
            int result = 10/0;
        }catch (Exception ex){
            throw new YYGHException(20002,"预约挂号异常");
        }

        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list",list);
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据医院设置的id删除医院设置信息")
    public R delete(@ApiParam(name = "id",value = "医院设置id",required = true)@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

}

